package validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.tastyshop.domain.Member;

public class JoinValidator implements Validator{

	public boolean supports(Class<?> clazz){
		return Member.class.isAssignableFrom(clazz);
	}
	
	public void validate(Object target, Errors errors){
		Member member = (Member) target;
		
		if(member.getEmail() == null || member.getEmail().trim().isEmpty()){
			errors.rejectValue("email", "error.member.email");
		}
		
		if(member.getUser_name() == null || member.getUser_name().trim().isEmpty()){
			errors.rejectValue("user_name", "error.member.user_name");
		}
		
		if(member.getNick_name() == null || member.getNick_name().trim().isEmpty()){
			errors.rejectValue("nick_name", "error.member.nick_name");
		}
		
		if(member.getUser_pw() == null || member.getUser_pw().trim().isEmpty()){
			errors.rejectValue("user_pw", "error.member.pw");
		}
		
		int pw_length = member.getUser_pw().length();
		if(pw_length < 4 || pw_length >13){
			errors.rejectValue("user_pw", "error.member.pw_length");
		}
		
		if(member.getMobile() == null || member.getMobile().trim().isEmpty()){
			errors.rejectValue("mobile", "error.member.mobile");
		}
		
		int mobile_length = member.getMobile().length();
		if(mobile_length < 10 || mobile_length > 11){
			errors.rejectValue("mobile", "error.member.mobile_length");
		}
	}

}
